<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/12
 * Time: 21:05
 */

class Comments extends WX_Controller
{

    const PAGE_SIZE = 20;

    /**
     * 初始化
     * Comment constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取评论列表
     */
    public function getComments()
    {
        if (!empty($this->openId)) {

            $page = $this->input->get("page");
            $goodsId = $this->input->get('goodsId');
            $page = !empty($page) && intval($page) > 0 ? $page : 1;
            $offset = ($page - 1) * self::PAGE_SIZE;

            if (empty($goodsId)) {
                $this->outputError('参数错误');
            }

            $this->load->model('common/Comment_Model', 'comment');
            $comments = $this->comment->lists($goodsId, $offset, self::PAGE_SIZE);
            $total = $this->comment->total();
            $this->output([
                'total' => $total,
                'data' => $comments,
                'next' => ($page + 1) * self::PAGE_SIZE < $total
            ]);

        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }
}

